<?php

namespace App\Http\Controllers\Upload;

use App\Http\Controllers\Controller;
use App\Models\SystemAttachment;
use App\Models\SystemAttachmentCategory;
use App\Traits\Controllers\DestroyTraits;
use App\Traits\Controllers\IndexTraits;
use App\Transformers\System\SystemAttachmentTransformer;
use Illuminate\Http\Request;

class SystemAttachmentController extends Controller
{
    use IndexTraits,DestroyTraits;

    public $model = SystemAttachment::class;
    public $transformer = SystemAttachmentTransformer::class;

    public function move(SystemAttachment $attachment, Request $request)
    {
        $attachment->category_id = $request->category_id;
        $attachment->save();

        return $this->response->noContent();
    }

    public function rename(SystemAttachment $attachment, Request $request)
    {
        $attachment->name = $request->name;
        $attachment->save();

        return $this->response->noContent();
    }

    public function uploads(SystemAttachment $attachment, Request $request)
    {
        $type = config('filesystems.default');
        if($type == 'oss'){
            $Token = new \App\Servers\Uploads\Oss();
        }else if ($type == 'kodo'){
            $Token = new \App\Servers\Uploads\Kodo();
        };

        $request->validate([
            'category_id' => 'required',
            'name' => 'required',
            'size' => 'required',
            'type' => 'required|in:image/jpg,image/jpeg,image/png,image/gif',
            'hash' => 'required',
            'upload_type' => 'required|in:oss, cos, Kodo, local',
            'module_type' => 'required|in:admin, user',
            'original_name' => 'required',
            'user_id' => 'required'
        ], [
            'name.required' => '附件名称 不能为空',
            'size.required' => '附件大小 不能为空',
            'type.required' => '附件类型 不能为空',
            'hash.required' => '哈希值 不能为空',
            'type.in' => '附件类型 必须为 image/jpg, image/jpeg, image/png, image/gif',
            'upload_type.required' => '上传云类型 不能为空',
            'upload_type.in' => '上传云类型 必须为OSS, COS, Kodo, local',
            'module_type.required' => '上传地址类型 不能为空',
            'module_type.in' => '上传人类型 必须为admin,user',
            'original_name.required' => '文件原名 不能为空',
            'user_id.required' => '用户id 不能为空',
        ], [
            'category_id' => '分类id',
            'name' => '文件名',
            'size' => '文件大小',
            'type' => '文件类型',
            'hash' => '哈希值',
            'upload_type' => '上传云类型',
            'module_type' => '上传人类型',
            'original_name' => '文件原名',
            'user_id' => '用户id',
        ]);

        $content = $request->input();

        $content['att_dir'] = render($content['name']);

        $data = $this->getQuery()::where('hash',$content['hash'])
            ->where('upload_type',$content['upload_type'])
            ->first();

        if (!empty($data)){
            return $content;
        }

        $attachment->fill($content);

        $this->response->created();

        return $Token->token();
    }

}


//class SystemAttachmentController extends Controller
//{
//    use IndexTraits,DestroyTraits;
//
//    public $model = SystemAttachment::class;
//    public $transformer = SystemAttachmentTransformer::class;
//
//    public function uploads(SystemAttachmentCategory $attachment, Request $request)
//    {
//        $type = config('filesystems.default');
//        if($type == 'oss'){
//            $Token = new \App\Servers\Uploads\Oss();
//        }else if ($type == 'kodo'){
//            $Token = new \App\Servers\Uploads\Kodo();
//        };
//
//        $request->validate([
//            'content.*.name' => 'required',
//            'content.*.size' => 'required',
//            'content.*.type' => 'required|in:image/jpg,image/jpeg,image/png,image/gif',
//            'content.*.hash' => 'required',
//            'content.*.upload_type' => 'required|in:OSS, COS, Kodo, local',
//            'content.*.module_type' => 'required|in:admin, user',
//            'content.*.original_name' => 'required',
//            'content.*.user_id' => 'required'
//        ], [
//            'name.required' => '附件名称 不能为空',
//            'size.required' => '附件大小 不能为空',
//            'type.required' => '附件类型 不能为空',
//            'hash.required' => '哈希值 不能为空',
//            'type.in' => '附件类型 必须为 image/jpg, image/jpeg, image/png, image/gif',
//            'upload_type.required' => '上传云类型 不能为空',
//            'upload_type.in' => '上传云类型 必须为OSS, COS, Kodo, local',
//            'module_type.required' => '上传地址类型 不能为空',
//            'module_type.in' => '上传人类型 必须为admin,user',
//            'original_name.required' => '文件原名 不能为空',
//            'user_id.required' => '用户id 不能为空',
//        ], [
//            'name' => '文件名',
//            'size' => '文件大小',
//            'type' => '文件类型',
//            'hash' => '哈希值',
//            'upload_type' => '上传云类型',
//            'module_type' => '上传人类型',
//            'original_name' => '文件原名',
//            'user_id' => '用户id',
//        ]);
//
//        $content = $request->input('content');
//        if (empty($content)){
//            return '请上传图片';
//        }
//
//        $res = [];
//
//        foreach ($content as $index => $item) {
//
//            $a = SystemAttachment::where('hash',$item['hash'])
//                ->where('type',$item['type'])
//                ->first();
//
//            if (empty($a)){
//                $res[$index] = $item;
//                $res[$index]['att_dir'] = renderOss($item['name']);
//            }
//        }
//
//        if (empty($res)){
//            return '';
//        }
//
//        $attachment->systemAttachment()->createMany($res);
//
//        $this->response->created();
//
//        return $Token->token();
//    }
//
//}
